package com.dsp.web;

import java.util.ArrayList;


import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.core.util.Constants;
import com.core.util.DspUtil;
import com.core.util.Pagination;
import com.core.util.PinYin;
import com.dsp.bean.Company;
import com.dsp.bean.Message;
import com.dsp.services.CompanyService;

@Controller("CompanysController")
public class CompanysController extends DspController<Company>{
	
	public static String[] QueryColumn = {"ccode","cname","cMethod","cType","orgCode","contact"};
	public static String[] QueryType   = {"String","String","String","String","String","String"};
	
	public CompanysController() {
		super(Company.class);
	}

	Logger logger = Logger.getLogger(DspUtil.Log4J);
	@Autowired
	private CompanyService companyService;

	@RequestMapping(value = "/companys", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("company/list");
		mav.addObject("path", "company/list.jsp");
		mav.addObject("current", "companys");
		return mav;
	}

	@RequestMapping(value = "/companys_list")
	@ResponseBody
	public Pagination<Company> index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//初始化Http对象  必须操作
		super.initHttp(request, response); 
		DetachedCriteria dc = DetachedCriteria.forClass(Company.class);
		super.getPageParameters(dc, QueryColumn	, QueryType);
		Pagination<Company> p = this.companyService.search(dc, (page - 1) * rp, rp);
		p.setPage(page);
		return p;
	}

	@RequestMapping(value = "/companys_create", method = RequestMethod.POST)
	@ResponseBody
	public Message create(@RequestBody Company company) {
		Message message = new Message();
		logger.info("正在创建....");
		try {
			company.setPyCode(new PinYin().toPinYin(company.getCname(), true, false));
			if (company.getId() != null) {
				this.companyService.update(company);
			} else {
				//company.setCcode(DspRandom.getCompanyNo("C"));
				this.companyService.save(company);
			}
		} catch (Exception se) {
			se.getStackTrace();
			message.setSuccess(false);
			List<String> msg = new ArrayList<String>();
			msg.add(se.getLocalizedMessage());
			message.setMsg(msg);
		}
		return message;
	}

	@RequestMapping(value = "/companys_edit", method = RequestMethod.POST)
	@ResponseBody
	public Company edit(@RequestBody Company _company) {
		Company company = this.companyService.findById(_company.getId());
		return company;
	}

	@RequestMapping(value = "/companys_delete", method = RequestMethod.POST)
	@ResponseBody
	public Message delete(@RequestBody Company company) {
		Message message = new Message();
		String error = this.companyService.delete(company);
		if (error != null){
			message.setSuccess(false);
			List<String> msg = new ArrayList<String>();
			msg.add(error);
			message.setMsg(msg);
		}
		return message;
	}

	@RequestMapping(value = "/companys_sss")
	@ResponseBody
	public List<Company> sss() {
		DetachedCriteria dc = DetachedCriteria.forClass(Company.class);
		List<Company> list = this.companyService.likeBy(dc);
		return list;
	}

	/**
	 * @return the companyService
	 */
	public CompanyService getCompanyService() {
		return companyService;
	}

	/**
	 * @param companyService
	 *            the companyService to set
	 */
	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

}
